import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 工作台
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '工作台', icon: 'dashboard' }
    }]
  },
  // 基础信息管理
  {
    path: '/management',
    component: Layout,
    redirect: '/management/warehouse.vue',
    name: 'Management',
    meta: { title: '基础信息管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'warehouse',
        name: 'Warehouse',
        component: () => import('@/views/management/warehouse.vue'),
        meta: { title: '仓库管理', icon: 'table' }
      },
      {
        path: 'area',
        name: 'Area',
        component: () => import('@/views/management/area.vue'),
        meta: { title: '库区管理', icon: 'table' }
      },
      {
        path: 'location',
        name: 'Location',
        component: () => import('@/views/management/location.vue'),
        meta: { title: '库位管理', icon: 'table' }
      },
      {
        path: 'view',
        name: 'View',
        component: () => import('@/views/management/view.vue'),
        meta: { title: '库位视图', icon: 'table' }
      },
      {
        path: 'kids',
        name: 'Kids',
        component: () => import('@/views/management/kids.vue'),
        meta: { title: '货品管理', icon: 'table' }
      },
      {
        path: 'tapeof',
        name: 'Tapeof',
        component: () => import('@/views/management/tapeof.vue'),
        meta: { title: '货品类型管理', icon: 'table' }
      }
    ]
  },
  // 商务管理
  {
    path: '/form',
    component: Layout,
    redirect: '/form/index',
    name: 'Form',
    meta: { title: '商务管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'owner',
        name: 'Owner',
        component: () => import('@/views/form/owner.vue'),
        meta: { title: '货主管理', icon: 'form' }
      },
      {
        path: 'carrier',
        name: 'Carrier',
        component: () => import('@/views/form/carrier.vue'),
        meta: { title: '承运商管理', icon: 'form' }
      }
    ]
  },
  // 库内管理
  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: '库内管理',
      icon: 'nested'
    },
    children: [
      {
        path: 'menu1',
        name: 'Menu1',
        component: () => import('@/views/nested/menu1.vue'),
        meta: { title: '盘点单', icon: 'form' }
      },
      {
        path: 'men2',
        name: 'Picker',
        component: () => import('@/views/nested/menu2.vue'),
        meta: { title: '盘点任务', icon: 'form' }
      },
      {
        path: 'menu3',
        name: 'Connect',
        component: () => import('@/views/nested/menu3.vue'),
        meta: { title: '库存损益', icon: 'form' }
      },
      {
        path: 'menu4',
        name: 'Connect',
        component: () => import('@/views/nested/menu4.vue'),
        meta: { title: '实时库存', icon: 'form' }
      }
    ]
  },
  // 入库管理
  {
    path: '/enterbound',
    component: Layout,
    redirect: '/enterbound/entry.vue',
    name: 'enterbound',
    meta: { title: '入库管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'entry',
        name: 'Entry',
        component: () => import('@/views/enterbound/entry.vue'),
        meta: { title: '入库单', icon: 'form' }
      },
      {
        path: 'take',
        name: 'Take',
        component: () => import('@/views/enterbound/take.vue'),
        meta: { title: '收货任务', icon: 'form' }
      },
      {
        path: 'putaway',
        name: 'Putaway',
        component: () => import('@/views/enterbound/putaway.vue'),
        meta: { title: '上架任务', icon: 'form' }
      }
    ]
  },
  // 出库管理
  {
    path: '/outbound',
    component: Layout,
    redirect: '/outbound/order.vue',
    name: 'outbound',
    meta: { title: '出库管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'order',
        name: 'Order',
        component: () => import('@/views/outbound/order.vue'),
        meta: { title: '出库单', icon: 'form' }
      },
      {
        path: 'picker',
        name: 'Picker',
        component: () => import('@/views/outbound/picker.vue'),
        meta: { title: '拣货任务', icon: 'form' }
      },
      {
        path: 'connect',
        name: 'Connect',
        component: () => import('@/views/outbound/connect.vue'),
        meta: { title: '交接任务', icon: 'form' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
